import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
import '../util/diagnostics_builder.dart';

part 'app_upgrade.g.dart';

var updaterJson = {
  // android 版本
  "android-version": "2.1.1",
  // -2 外部更新(意外情况启动web下载更新), -1 强制更新, 0 正常提示更新, 1 弱提示更新(如小红点提示), 2 隐藏更新(内侧更新等)
  "android-type": 0,
  // 最小支持版本，超过该版本执行强制更新
  "android-min": "2.0.0",
  // rev审核使用 0 未审核， 1 审核中
  "android_rev": {"rev": 1, "code": "124"},
  // android 信息
  "android-desc": "1.APP Loan Upgrade \n2.Optimize experience & fix the BUG",
  // android 下载地址
  "android-download-url":
      "https://app.crypts.cn/appDownload/singular_fire_app_release.apk",
  // ios 版本
  "ios-version": "2.1.0",
  // 同android-type
  "ios-type": 0,
  // 同android-min
  "ios-min": "2.0.0",
  // rev审核使用 0 未审核， 1 审核中
  "ios_rev": {"rev": 1, "code": "124"},
  // 同android-desc
  "ios-desc": "1.APP Loan Upgrade \n2.Security upgrade:Add Device Management",
  // ios appstore 连接
  "ios-download-url": "https://apps.apple.com/cn/app/id1575026717",
  // web h5 下载连接
  "web_link": "https://crypts.cn/page/download.html",
  // 内测 下载连地址
  "test-url": "https://app.crypts.cn/appDownload/singular_fire_app_release.apk"
};

@JsonSerializable(fieldRename: FieldRename.kebab)
class AppUpgrade {
  const AppUpgrade({
    required this.downloadUrl,
    required this.androidVersion,
    // required this.androidType,
    // required this.androidMin,
    required this.androidDesc,
    required this.iosVersion,
    // required this.iosType,
    // required this.iosMin,
    required this.iosDesc,
    required this.iosDownloadUrl,
    // required this.testUrl,
    required this.aRev,
    required this.iRev,
  });

  factory AppUpgrade.fromJson(Map<String, dynamic> json) =>
      _$AppUpgradeFromJson(json);

  final String downloadUrl;
  final String androidVersion;
  // final int androidType;
  // final String androidMin;
  final List<dynamic> androidDesc;
  final String iosVersion;
  // final int iosType;
  // final String iosMin;
  final List<dynamic> iosDesc;
  final String iosDownloadUrl;
  // final String testUrl;
  final AppRev aRev;
  final AppRev iRev;

  Map<String, dynamic> toJson() => _$AppUpgradeToJson(this);

  @override
  String toString() {
    return DiagnosticsBuilder(type: AppUpgrade)
        .addProperty('androidUrl', downloadUrl)
        .addProperty('androidVersion', androidVersion)
        // .addProperty('androidType', androidType)
        // .addProperty('androidMin', androidMin)
        .addProperty('androidDesc', androidDesc)
        .addProperty('iosVersion', iosVersion)
        // .addProperty('iosType', iosType)
        // .addProperty('iosMin', iosMin)
        .addProperty('iosDesc', iosDesc)
        .addProperty('iosUrl', iosDownloadUrl)
        // .addProperty('testUrl', testUrl)
        .addProperty('androidRev', aRev.toJson())
        .addProperty('iosRev', iRev.toJson())
        .toString();
  }
}

class AppRev {
  int? rev;
  String? code;

  AppRev({this.rev, this.code});

  AppRev.fromJson(Map<String, dynamic> json) {
    rev = json['is_rev'];
    code = json['version_code'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['is_rev'] = this.rev;
    data['version_code'] = this.code;
    return data;
  }
}

extension AppUpgradeExt on AppUpgrade {
  Future<String> matchApkUrl() {
    if ('androidType' == 2) {
      return SynchronousFuture<String>('testUrl');
    }

    return DeviceInfoPlugin().androidInfo.then((AndroidDeviceInfo androidInfo) {
      if (androidInfo.supported64BitAbis.isNotEmpty) {
        return downloadUrl;
      }
      return downloadUrl;
    });
  }
}
